package com.shujia.core

import com.shujia.core.Demo07GroupBy.StuGrp
import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.rdd.RDD

object Demo12Sample {
  def main(args: Array[String]): Unit = {

    val conf: SparkConf = new SparkConf()
    conf.setAppName("Demo12Sample")
    conf.setMaster("local")

    val sc: SparkContext = new SparkContext(conf)

    val stuLineRDD: RDD[String] = sc.textFile("spark/data/students.txt")

    // 将每条数据转换成样例类对象
    val stuRDD: RDD[StuGrp] = stuLineRDD.map(line => {
      val splits: Array[String] = line.split(",")
      StuGrp(splits(0), splits(1), splits(2).toInt, splits(3), splits(4))
    })

    /**
     * sample：主要用于对数据随机取样
     * withReplacement: Boolean 是否放回
     * fraction: Double 取样的比例（只是大概，不是精确）
     * seed: Long = Utils.random.nextLong 随机数种子 如果需要让每次取样的结果一致，则可将其固定
     */

    stuRDD.sample(withReplacement = false, fraction = 0.1)
    //      .foreach(println)
    stuRDD.sample(withReplacement = false, fraction = 0.01, 1).foreach(println)

  }

}
